package offerbook;

import utils.TreeNode;
import utils.TreeUtil;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @date 2020/3/9 14:02
 */
public class Code19_MirrorBT {
    public static void Mirror(TreeNode root) {
        if(root == null) return;
        //每次遍历到一个节点，就交换节点左右孩子
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if(root.left != null)
            Mirror(root.left);
        if(root.right != null)
            Mirror(root.right);
    }
    //level traverse
    public static void Mirror2(TreeNode root) {
        if(root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            root = queue.poll();
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            if(root.left != null){
                queue.offer(root.left);
            }
            if(root.right != null){
                queue.offer(root.right);
            }
        }
    }

    public static void main(String[] args) {
        //8,10,6,11,9,7,5
        TreeNode root = new TreeNode(8);
        root.left = new TreeNode(10);
        root.right = new TreeNode(6);
        root.left.left = new TreeNode(11);
        root.left.right = new TreeNode(9);
        root.right.left = new TreeNode(7);
        root.right.right = new TreeNode(5);
        TreeUtil.printTree(root);
        Mirror2(root);
        TreeUtil.printTree(root);
    }
}
